//
//  VoicePlayViewController.m
//  RadioUrl
//
//  Created by ios on 16/4/22.
//  Copyright © 2016年 NIIT. All rights reserved.
//

#import "VoicePlayViewController.h"
#import <AFNetworking.h>
#import <TFHpple.h>
#import "PlayView.h"
#import "RadioModel.h"
#import "ModelSingleton.h"
#import "VoicePlayer.h"
#import "NetWorking.h"
@interface VoicePlayViewController ()<PlayViewDelegate>
@property(nonatomic,strong) NSMutableArray *radioModelList;
@property(nonatomic,strong) PlayView *playView;
@property(nonatomic,copy) NSString *soundIDs;
@property(nonatomic,assign) NSInteger currentIndex;
@property(nonatomic,strong) RadioModel *currentRadioModel;
@property(nonatomic,assign) BOOL isPlay;
@property(nonatomic,strong) NSMutableArray *mArr;


@end

@implementation VoicePlayViewController
-(void)loadView{
    [super loadView];
    
//    [self radioModelListWithSoundIDs:[VoicePlayer shareVoicePlayer].currentRadioModel.radioSoundId
//     ];
//    NSLog(@"%@",self.mArr);
}



- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.playView = [[[NSBundle mainBundle]loadNibNamed:@"VoicePlayViewController" owner:self options:nil]lastObject];
    
    self.playView.radioImage.image = [UIImage imageNamed:@"22"];
    self.playView.frame =self.view.bounds;
    
    self.view = self.playView;
    
    self.playView.delegate = self;
    [VoicePlayer shareVoicePlayer].delegate = self;
    
   self.currentRadioModel = [VoicePlayer shareVoicePlayer].currentRadioModel;
    self.currentIndex = [VoicePlayer shareVoicePlayer].curentIndex;
    self.soundIDs = [VoicePlayer shareVoicePlayer].currentRadioModel.radioSoundId;
    NSArray *radioSoundIDs = [self.soundIDs componentsSeparatedByString:@","];
    
    self.mArr = radioSoundIDs;
    [self networkingWithUrl:self.mArr[self.currentIndex]];
    
//     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(nextBtnPressed) name:AVPlayerItemDidPlayToEndTimeNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(nextBtnPressed) name:AVPlayerItemDidPlayToEndTimeNotification object:nil];

}

#pragma mark实现VoicePlayer的代理方法
-(void)sendSongProgressMessage:(CGFloat)songProgress{
   
    self.playView.sliderView.value = songProgress;
    NSString *leftTime = [NSString stringWithFormat:@"%0.2d:%0.2d",(int)(songProgress / 60),((int)songProgress % 60)];
    self.playView.timeLabel.text = leftTime;
    
}


//根据soundID获取currentRadioModel
-(void)networkingWithUrl:(NSString *)radioSource{
    [NetWorking searchWithJsonUrl:radioSource success:^(RadioModel *radioModel) {
        [VoicePlayer shareVoicePlayer].currentRadioModel = radioModel;
        self.currentRadioModel.radioTitle = radioModel.radioTitle;
        self.currentRadioModel.radioTimeLength = radioModel.radioTimeLength;
        self.currentRadioModel.radioPath = radioModel.radioPath;
        int endTime = [self.currentRadioModel.radioTimeLength intValue];
    
        NSString *timeLength = [NSString stringWithFormat:@"%0.2d:%0.2d",endTime / 60,endTime% 60];
        
        self.playView.sliderView.maximumValue = endTime;
        NSLog(@"+++++++%@",radioModel.radioImage);
        [[VoicePlayer shareVoicePlayer] play];
        [self.playView playViewWithImageName:self.currentRadioModel.radioImage radioName:self.currentRadioModel.radioTitle endTime:timeLength];
}
     
  failure:^(NSError *error) {
      
  }];
}
#pragma mark playView播放代理的实现
-(void)backBtnPressed{
    [[VoicePlayer shareVoicePlayer].player pause];
    [self.playView.timer invalidate];

    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}
//播放或暂停
-(void)playBtnPressed{
    self.isPlay = !self.isPlay;
    if (self.isPlay) {
        self.playView.playButton.selected = YES;
        [self.playView.playButton setImage:[UIImage imageNamed:@"play"] forState:UIControlStateSelected];
 
    }else{
        self.playView.playButton.selected = NO;
 
    }
    
    [[VoicePlayer shareVoicePlayer] playeBtnPressing];
    
       
}

-(void)preBtnPressed{
    self.currentIndex--;
    if (self.currentIndex <0) {
        self.currentIndex = 0;
    }
    
    [self networkingWithUrl:self.mArr[self.currentIndex]];
}

-(void)nextBtnPressed{
    self.currentIndex++;
    if (self.currentIndex > self.mArr.count) {
        self.currentIndex = 0;
    }
    [self networkingWithUrl:self.mArr[self.currentIndex]];
  }
-(void)sliderSeekToTime{
    [[VoicePlayer shareVoicePlayer] seekPlayerToTime:self.playView.sliderView.value];
}
    @end
